package cn.melonlib.nars.record.comp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.melonlib.cores.aop.anno.inject.FilterRecord;
import cn.melonlib.cores.aop.anno.inject.FilterTenant;
import cn.melonlib.cores.aop.anno.inject.InjectRecord;
import cn.melonlib.cores.aop.anno.inject.InjectTenant;
import cn.melonlib.cores.aop.comp.web.WebInfo;
import cn.melonlib.cores.commons.comp.enums.OrderByType;
import cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService;
import cn.melonlib.cores.identification.comp.messenger.MessengerUtils;
import cn.melonlib.cores.identification.model.entity.Identification;
import cn.melonlib.cores.identification.model.entity.Organization;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import cn.melonlib.cores.jpa.model.bean.impl.OffsetQueryImpl;
import cn.melonlib.cores.jpa.model.bean.impl.PaginationList;
import cn.melonlib.cores.model.anno.*;
import cn.melonlib.nars.names.comp.service.NamesService;
import cn.melonlib.nars.names.model.entity.MelonNames;
import cn.melonlib.nars.record.model.entity.NamesOperatorRecord;
import cn.melonlib.nars.record.model.enums.OperatorType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import static cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService.ROOT_METHOD.*;
@WorkerComment("资源操作日志")
@Service
@WorkerService(
        module = "records"
        ,model = @WorkerModel(
                modelType = NamesOperatorRecord.class
)
        ,ignores = {SAVE,SEARCH,REMOVE_BY_ID_LIST,REMOVE_BY_ID,LIST,LOAD_ALL,TOTAL_COUNT,SAVE}
)
@InjectRecord
@InjectTenant
@FilterTenant
@FilterRecord
public class NamesOperatorRecordService extends WorkerBasicService<NamesOperatorRecord> {

    @Resource
    private NamesService namesService;

    @Resource
    private MessengerUtils messengerUtils;

    @Resource
    private WebInfo webInfo;

    @WorkerStore(successTip = "添加成功",errorTip = "添加失败")
    @WorkerComment("添加操作日志")
    @Transactional(value = Transactional.TxType.REQUIRED,rollbackOn = Exception.class)
    public NamesOperatorRecord addRecord(
            @WorkerParameter(value = "path",comment = "资源路径",required = true) String path,
            @WorkerParameter(value = "operatorType",comment = "操作类型",required = true) OperatorType operatorType
    )throws Exception{
        MelonNames names=namesService.lookup(path);
        NamesOperatorRecord namesOperatorRecord=new NamesOperatorRecord();
        messengerUtils.setMessengers(namesOperatorRecord);
        namesOperatorRecord.setPath(names.getPath());
        namesOperatorRecord.setOperatorType(operatorType);
        namesOperatorRecord.setIp(webInfo.getClientInfo().getIp());
        namesOperatorRecord.setBody(getJsonMapper().writeValueAsString(webInfo.getClientInfo()));
        return dao.save(namesOperatorRecord).getResult();
    }


    public PaginationList<NamesOperatorRecord> listRecord(
            @WorkerParameter(value = "path",comment = "资源路径",required = true) String path,
            @WorkerParameter(value = "first",comment = "数量",required = true) Integer first,
            @WorkerParameter(value = "offset",comment = "起始",required = true) Long offset
            ){
        MelonNames names=namesService.lookup(path);
        Identification identification=getIdentificationMessenger().getIdentification();
        Organization organization=getIdentificationMessenger().getOrganization();
        return this.dao.<PaginationList<NamesOperatorRecord>,OffsetQueryImpl>search(new OffsetQueryImpl(NamesOperatorRecord.class,
        JsonMap.<JsonMap<Object>>createMap().append("path",JsonMap.createMap().append("eq",names.getPath()))
                .append("creatorId",JsonMap.createMap().append("eq", BeanUtil.getProperty(identification,"id")))
                .append("tenantId",JsonMap.createMap().append("eq",BeanUtil.getProperty(organization,"id")))
                ,JsonMap.<OrderByType>createMap().append("createDateTime", OrderByType.DESC)
                ,offset,first
        )).getResult();
    }
}
